package fr.lip6.meta.ple.generation.aspectj;

import org.eclipse.emf.common.util.EList;
import org.eclipse.gmt.modisco.java.*;

import fr.lip6.meta.ple.generation.strategy.ReadXmiStrategy;

public class ReadXmi extends ReadXmiStrategy {

	@Override
	public String getMethodDeclarationAndBodyText(MethodDeclaration methodDecl) {
		StringBuilder res = new StringBuilder();
		String returntype = methodDecl.getReturnType().getType().getName();
		EList<Statement> sts;
		org.eclipse.gmt.modisco.java.Block blck;

		res.append(getMethodDeclarationText(methodDecl));
		res.append(" {\n");

		blck = methodDecl.getBody();

		sts = blck.getStatements();

		for(Statement st : sts) {
			res.append(getStatement(st));
			res.append("\n");
		}

		if (returntype.equals("void")) {
			res.append("\t}");
		} else {
			res.append("\t}");
		}

		return res.toString();
	}

	@Override
	public String getMethodDeclarationText(MethodDeclaration methodDecl) {
		StringBuilder res = new StringBuilder();
		String vis = getMethodModifierTextWithSpace(methodDecl);
		String returntype = methodDecl.getReturnType().getType().getName();
		EList<SingleVariableDeclaration> parameters = methodDecl.getParameters();
		String method = vis+" "+returntype+" "+methodDecl.getName()+"(";
		int i = 0;

		if (parameters.size() == 0) {
			method += ")";
		} else {
			for(SingleVariableDeclaration var : parameters) {
				i++;
				if (i != parameters.size()) {
					method += var.getType().getType().getName() +" "+var.getName()+", ";
				} else {
					method += var.getType().getType().getName() +" "+var.getName()+")";
				}
			}
		}

		res.append(method);

		return res.toString();
	}

	@Override
	public String getConstructorDeclarationAndBodyText(ConstructorDeclaration consDecl) {
		StringBuilder res = new StringBuilder();
		// String returntype = m.getReturnType().getType().getName();
		EList<Statement> sts;

		res.append(getConstructorDeclarationText(consDecl));
		res.append(" {\n");

		org.eclipse.gmt.modisco.java.Block block = consDecl.getBody();

		sts = block.getStatements();

		for(Statement st : sts) {
			res.append(getStatement(st));
			res.append("\n");
		}

		res.append("\t}");

		return res.toString();
	}

	@Override
	public String getConstructorDeclarationText(ConstructorDeclaration cons) {
		StringBuilder res = new StringBuilder();
		String vis = getConstructorModifierTextWithSpace(cons);
		EList<SingleVariableDeclaration> parameters = cons.getParameters();
		String consDecl = vis+" "+cons.getName()+"(";
		int i=0;

		if(parameters.size() == 0) {
			consDecl += ")";
		} else {
			for(SingleVariableDeclaration var : parameters) {
				i++;
				if(i != parameters.size()) {
					consDecl += var.getType().getType().getName() +" "+var.getName()+", ";
				} else {
					consDecl += var.getType().getType().getName() +" "+var.getName()+")";
				}
			}
		}

		res.append(consDecl);

		return res.toString();
	}
}